
package gui;
import business.Categoria;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import vo.CategoriaVO;



public class Categoria_Pesquisar extends javax.swing.JFrame {

    public Categoria_Pesquisar() {
        initComponents();
        updateCategoria();
    }
    
    @SuppressWarnings("unchecked")
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelCategoriaPesquisarBusca = new javax.swing.JLabel();
        jTextFieldCategoriaPesquisarBusca = new javax.swing.JTextField();
        jButtonCategoriaPesquisarSelecionar = new javax.swing.JButton();
        jScrollPaneCategoriaPesquisar = new javax.swing.JScrollPane();
        jTableCategoriaPesquisarTabela = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("gui/Bundle"); // NOI18N
        jLabelCategoriaPesquisarBusca.setText(bundle.getString("Categoria_Pesquisar.jLabelCategoriaPesquisarBusca.text")); // NOI18N

        jTextFieldCategoriaPesquisarBusca.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldCategoriaPesquisarBuscaFocusLost(evt);
            }
        });

        jButtonCategoriaPesquisarSelecionar.setText(bundle.getString("Categoria_Pesquisar.jButtonCategoriaPesquisarSelecionar.text")); // NOI18N
        jButtonCategoriaPesquisarSelecionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCategoriaPesquisarSelecionarActionPerformed(evt);
            }
        });

        jTableCategoriaPesquisarTabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nome", "Tarifa"
            }
        ));
        jTableCategoriaPesquisarTabela.getTableHeader().setReorderingAllowed(false);
        jScrollPaneCategoriaPesquisar.setViewportView(jTableCategoriaPesquisarTabela);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel1.setText(bundle.getString("Categoria_Pesquisar.jLabel1.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabelCategoriaPesquisarBusca, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextFieldCategoriaPesquisarBusca))
                            .addComponent(jScrollPaneCategoriaPesquisar, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButtonCategoriaPesquisarSelecionar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(159, 159, 159)
                        .addComponent(jLabel1)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldCategoriaPesquisarBusca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelCategoriaPesquisarBusca))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPaneCategoriaPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButtonCategoriaPesquisarSelecionar, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonCategoriaPesquisarSelecionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCategoriaPesquisarSelecionarActionPerformed
        String nome = (String) jTableCategoriaPesquisarTabela.getValueAt(jTableCategoriaPesquisarTabela.getSelectedRow(),0);
        String tarifa = Float.toString((float) jTableCategoriaPesquisarTabela.getValueAt(jTableCategoriaPesquisarTabela.getSelectedRow(),1));
        Categoria_Principal.getInstance().setjTextFieldCategoriaPrincipalNome(nome);
        Categoria_Principal.getInstance().setjTextFieldCategoriaPrincipalTarifa(tarifa);
        Categoria_Principal.getInstance().setsStatus(Categoria_Principal.StatusCategoriaPrincipal.scPesquisado);
        Categoria_Principal.getInstance().setjTextFieldCategoriaPrincipalNomeEnabled(false);
        dispose();
    }//GEN-LAST:event_jButtonCategoriaPesquisarSelecionarActionPerformed

    private void jTextFieldCategoriaPesquisarBuscaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldCategoriaPesquisarBuscaFocusLost

String nome= jTextFieldCategoriaPesquisarBusca.getText();

  if(!Categoria.categoriaExiste(nome)||jTextFieldCategoriaPesquisarBusca.getText().isEmpty())
  {
    if(jTextFieldCategoriaPesquisarBusca.getText().isEmpty())
    {
           ((DefaultTableModel) jTableCategoriaPesquisarTabela.getModel()).setNumRows(0);
            jTableCategoriaPesquisarTabela.updateUI();       
        updateCategoria();
    }
    else
    {
     jTextFieldCategoriaPesquisarBusca.requestFocus();
      jTextFieldCategoriaPesquisarBusca.selectAll();
      return;
    }
  }
  else
  {
          ((DefaultTableModel) jTableCategoriaPesquisarTabela.getModel()).setNumRows(0);
            jTableCategoriaPesquisarTabela.updateUI();
            updateNome(nome); 
  }
         

        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldCategoriaPesquisarBuscaFocusLost
    public void updateNome(String nome)
    {
        List<CategoriaVO> lista = Categoria.getCategoriaPeloNome(nome);
        Object[][] dados = new Object[lista.size()][2];
        
        for(int i=0;i<1;i++){
            CategoriaVO categoria =(CategoriaVO) lista.get(i);
            dados[i][0]=categoria.getNome();
            dados[i][1]=categoria.getTarifa();
            ((DefaultTableModel)jTableCategoriaPesquisarTabela.getModel()).addRow(dados[i]);
        }
        
    }
    public void updateCategoria(){
        
        List<CategoriaVO> lista = Categoria.getCategorias();
        
        Object[][] dados = new Object[lista.size()][2];
        
        for(int i=0;i<lista.size();i++)
        {
            CategoriaVO categoria = (CategoriaVO) lista.get(i);
            dados[i][0]=categoria.getNome();
            dados[i][1]=categoria.getTarifa();
            ((DefaultTableModel)jTableCategoriaPesquisarTabela.getModel()).addRow(dados[i]);
        }

    }
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCategoriaPesquisarSelecionar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelCategoriaPesquisarBusca;
    private javax.swing.JScrollPane jScrollPaneCategoriaPesquisar;
    private javax.swing.JTable jTableCategoriaPesquisarTabela;
    private javax.swing.JTextField jTextFieldCategoriaPesquisarBusca;
    // End of variables declaration//GEN-END:variables
}
